<template>
  <div class="formData">
    <el-form ref="form" size="small" label-width="200px">
      <appointment-settings ref="appointmentSettingsRef" />
      <facility-and-service
        ref="facilityAndServiceRef"
      />
      <el-form-item>
        <el-button type="primary" round @click="submit">保存配置</el-button>
        <!--        <el-button round @click="reset">重置配置</el-button>-->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import AppointmentSettings from './AppointmentSettings'
import FacilityAndService from './FacilityAndService'
import { updateAppointmentConfig } from '@/api/reservation'
export default {
  name: 'FormData',
  components: { AppointmentSettings, FacilityAndService },
  data() {
    return {
    }
  },
  methods: {
    // 保存表单
    async submit() {
      const formData = await this.$refs.appointmentSettingsRef.getQuery()
      const facilityAndServiceRefFormData =
        await this.$refs.facilityAndServiceRef.getQuery()
      if (formData && facilityAndServiceRefFormData) {
        for (let index = 0; index < formData.maxReservationCapacity.length; index++) {
          if (!formData.maxReservationCapacity[index].max) {
            this.$message.error('请输入整数。最小为1，最大为9999,9999则为无限')
            return false
          }
        }
        const params = {
          allowModifyTime: formData.allowModifyTime,
          appointmentItem: formData.appointmentItem,
          businessStatus: formData.businessStatus,
          cancelOrderTime: formData.cancelOrderTime,
          cancelReservationRestrictions: formData.cancelReservationRestrictions,
          cancelReservationUnit: formData.cancelReservationUnit,
          initiateReservationRestrictions: formData.initiateReservationRestrictions,
          initiateReservationUnit: formData.initiateReservationUnit,
          maxReservationCapacity: formData.maxReservationCapacity,
          modificationTimes: formData.modificationTimes,
          personnelItem: formData.personnelItem,
          remindTime: formData.remindTime,
          remindUnit: formData.remindUnit,
          reservationScope: formData.reservationScope,
          shopId: formData.shopId,
          supplyFacility: facilityAndServiceRefFormData.supplyFacility,
          supplyService: facilityAndServiceRefFormData.supplyService,
          shopLabel: facilityAndServiceRefFormData.shopLabel,
          description: facilityAndServiceRefFormData.description
        }
        const { code } = await updateAppointmentConfig(params)
        if (code === 0) {
          this.$message.success('保存成功')
        }
        console.log('通过', params, code)
      }
    },
    setQuery(data) {
      this.$refs.appointmentSettingsRef.setQuery(data)
      this.$refs.facilityAndServiceRef.setQuery(data)
    },
    clearValidate() {
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style scoped lang="scss"></style>
